﻿@using System.Globalization
@using LERP.Domain.Enums
@using Sharp.Framework
@using Sharp.Framework.Extensions
@model LERP.Web.Controllers.ViewModels.ReturnOrderViewModels.ModifyReturnOrderViveModel
@{
    ViewBag.Title = "修改退单";
    Layout = "~/Views/Shared/_Layout.cshtml";
    var status = (ReturnOrderStatus)Model.Status;
    var templateString = "";
}
<div class="empty" style="text-align: left;background-color: #FFFCEB;font-size: 14px;font-family: 宋体,Arial,Lucida,Verdana,Helvetica,sans-serif;">
    
    <strong>退单编号: @this.ViewContext.RouteData.Values["id"] </strong>    <strong>状态: @(((ReturnOrderStatus)Model.Status).GetDescription())   </strong> 
</div>
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset> 
        <legend>基本信息 </legend>
        <ul class="list_form">
            <li>
                @Html.LabelFor(model => model.Customer)   
               @if (status == ReturnOrderStatus.Draft)
                {
                    @Html.TextBoxFor(model => model.Customer, new {@class = "text-long",@readonly ="readonly"})    <a href="@Url.Action("Select", "Customer")" id="customer_select" style="">选择客户</a>
                    @Html.ValidationMessageFor(model => model.Customer)
                }
                else
                {
                    @Model.Customer
                    @Html.HiddenFor(model => model.Customer)
                }  
                @Html.HiddenFor(model => model.CustomerId)
            </li> 
             <li>
                @Html.LabelFor(model => model.Contract)   
                @if (status == ReturnOrderStatus.Draft)
                {
                    @Html.DropDownListFor(m => m.Contract, (ViewBag.Contracts as List<SelectListItem>).InsertEmpty(), new { @class = "text-long" })
                    @Html.ValidationMessageFor(model => model.Contract)
                }
                else
                {
                    @Model.ContractName
                    @Html.HiddenFor(model => model.ContractName)
                }
                @Html.HiddenFor(model => model.Contract)
            </li>
            <li>
                @Html.LabelFor(model => model.ReturnDate)   
                @if (status == ReturnOrderStatus.Draft)
                {
                    @Html.TextBoxFor(model => model.ReturnDate, new { @class = "text-long dataselect" })
                    @Html.ValidationMessageFor(model => model.ReturnDate)
                }
                else
                {
                   @(string.IsNullOrEmpty(Model.ReturnDate)?"未填写":Model.ReturnDate)  
                    @Html.HiddenFor(model => model.ReturnDate)
                }  
            </li>
        
            <li>
                @Html.LabelFor(model => model.CustomerOrderNo)   
                @if (status == ReturnOrderStatus.Draft)
                {
                    @Html.TextBoxFor(model => model.CustomerOrderNo, new {@class = "text-long"}) 
                @Html.ValidationMessageFor(model => model.CustomerOrderNo)
                }
                else
                {
                    @(string.IsNullOrEmpty(Model.CustomerOrderNo)?"未填写":Model.CustomerOrderNo)  
                    @Html.HiddenFor(model => model.CustomerOrderNo)
                }  
            </li>
            <li>
                @Html.LabelFor(model => model.SafeNo)   
                @if (status == ReturnOrderStatus.Draft)
                {
                    @Html.TextBoxFor(model => model.SafeNo, new {@class = "text-long"}) 
                @Html.ValidationMessageFor(model => model.SafeNo)
                }
                else
                { 
                    @(string.IsNullOrEmpty(Model.SafeNo)?"未填写":Model.SafeNo)  
                    @Html.HiddenFor(model => model.SafeNo)
                }  
            </li>
            <li>
                @Html.LabelFor(model => model.DeliveryNo)   
                @if (status == ReturnOrderStatus.Draft)
                {
                    @Html.TextBoxFor(model => model.DeliveryNo, new {@class = "text-long"}) 
                @Html.ValidationMessageFor(model => model.DeliveryNo)
                }
                else
                { 
                    @(string.IsNullOrEmpty(Model.DeliveryNo)?"未填写":Model.DeliveryNo)  
                    @Html.HiddenFor(model => model.DeliveryNo)
                }  
            </li>
            <li>
                @Html.LabelFor(model => model.LogisticsProvider) 
                @if (status == ReturnOrderStatus.Draft)
                {
                    @Html.DropDownListFor(m => m.LogisticsProvider, (ViewBag.LogisticsProviders as List<SelectListItem>).InsertEmpty())
                @Html.ValidationMessageFor(model => model.LogisticsProvider)
                }
                else
                {
                    @(string.IsNullOrEmpty(Model.LogisticsProviderName)?"未填写":Model.LogisticsProviderName)   
                    @Html.HiddenFor(model => model.LogisticsProviderName)
                }  
            </li>
            <li>
                @Html.LabelFor(model => model.CarrierCode)   
                @if (status == ReturnOrderStatus.Draft)
                {
                    @Html.TextBoxFor(model => model.CarrierCode, new {@class = "text-long"}) 
                @Html.ValidationMessageFor(model => model.CarrierCode)
                }
                else
                { 
                    @(string.IsNullOrEmpty(Model.CarrierCode)?"未填写":Model.CarrierCode)   
                    @Html.HiddenFor(model => model.CarrierCode)
                }  
            </li>
           @if (status == ReturnOrderStatus.WaitForReceipt)
             {
                 <li>
                     @Html.LabelFor(model => model.WarehouseCode)   
                     @if (status == ReturnOrderStatus.WaitForReceipt)
                     {
                         @Html.TextBoxFor(model => model.WarehouseCode, new { @class = "text-long" })
                         @Html.ValidationMessageFor(model => model.WarehouseCode)
                     }
                     else
                     { 
                    @(string.IsNullOrEmpty(Model.WarehouseCode)?"未填写":Model.WarehouseCode)   
                         @Html.HiddenFor(model => model.WarehouseCode)
                     }
                 </li>
                 <li>
                     @Html.LabelFor(model => model.WarehouseAmount)   
                     @if (status == ReturnOrderStatus.WaitForReceipt)
                     {
                         @Html.TextBoxFor(model => model.WarehouseAmount, new { @class = "text-long" })
                         @Html.ValidationMessageFor(model => model.WarehouseAmount)
                     }
                     else
                     {
                         @Model.WarehouseAmount
                         @Html.HiddenFor(model => model.WarehouseAmount)
                     }
                 </li> 
             }
            <li>
                @Html.LabelFor(model => model.Remark)   
                @Html.TextAreaFor(model => model.Remark, new {rows="1" ,cols="1"})
                 @Html.ValidationMessageFor(model => model.Remark)
            </li>
            <li>
                <label>是否关联货品</label>  
                <input name="@Html.IdFor(m=>m.IsGoods)" id="@Html.IdFor(m=>m.IsGoods)" @((null!=Model&&Model.IsGoods)?"checked=\"checked\"":"") value="true"  type="checkbox" /> 
                @Html.LabelFor(model => model.IsGoods,new{style="text-align:left;"})
                @Html.ValidationMessageFor(model => model.Amount)
            </li>
            <li id="li_Amount" style="display: @((null!=Model&&Model.IsGoods)?"none":"inline");">
                @Html.LabelFor(model => model.Amount)   
                @Html.TextBoxFor(model => model.Amount, new {@class = "text-long"}) 
            </li>
        </ul> 
        
         
    </fieldset>
    <fieldset id="fieldset_goods" style="display: @((null!=Model&&Model.IsGoods)?"":"none");"> 
        <legend>货品明细</legend>
         @if (status == ReturnOrderStatus.Draft)
         {
             <a href="@Url.Action("Select", "Goods")" id="goods_select" style="float: right; margin-bottom: 5px;">选择</a>
         }
       <table class="table_list" id="goods_table" style="width: 99%;display: none;">
         <tr>
                <th style="width: 80px;">国际条码</th>
                <th>品名规格</th>
                <th style="width: 80px;">单价</th>
                <th style="width: 30px;">单位</th>
                <th style="width: 60px;">退货数量</th>
                 @if ( status == ReturnOrderStatus.Draft)
            {
                <th style="width: 30px;">操作</th>
            }
            </tr>              
     </table>
        <div style="float: right; margin-top: 25px; width: 300px; text-align: right;">
            <div style="margin-bottom: 5px;">退单金额: <label id="order_sum">0</label> 元 
                @Html.HiddenFor(model => model.OrderItems)
            </div> 
        </div>
    </fieldset>
        <input type="submit"  class="button-submit" value="保存" />
<br/>
<br/>
<br/>
}
@section scripts
{
    <script type="text/javascript">
        @switch (status)
        {
            case ReturnOrderStatus.Draft:
                {
                    templateString = "<tr id=\\\"goods_{0}\\\"><td>{1}</td><td title=\\\"{2}\\\">{3}</td><td><input class=\\\"text-small\\\" goodsid=\\\"{0}\\\"  name=\\\"price\\\" type=\\\"text\\\" value=\\\"{4}\\\"  olevalue=\\\"{4}\\\" /></td><td>{5}</td><td><input class=\\\"text-small\\\"  onkeyup=\\\"this.value=this.value.replace(/\\D/g,'')\\\" onafterpaste=\\\"this.value=this.value.replace(/\\D/g,'')\\\" goodsid=\\\"{0}\\\"  name=\\\"num\\\" type=\\\"text\\\" value=\\\"{6}\\\"></td><td class=\\\"action\\\"><a  goodsid=\\\"{0}\\\"   href=\\\"javascript:void(0);\\\">取消</a></td></tr>";
                    break;
                }
            default:
                {
                    templateString = "<tr id=\\\"goods_{0}\\\"><td>{1}</td><td title=\\\"{2}\\\">{3}</td><td>{4}</td><td>{5}</td><td>{6}</td></tr>";
                    break;
                }
        }
        
        var goodsitemtemplate = "@(new MvcHtmlString(templateString))";
        var goodsList = new Array();

        $(function () {
            //加载返回的订单项
            var $orderItems = $("#@Html.IdFor(m=>m.OrderItems)");
            if ($orderItems.val() != '') {
                goodsList = JSON.parse($orderItems.val());
                for (var i = 0; i < goodsList.length; i++) {
                    var item = goodsList[i];
                    $("#goods_table").append(String.format(goodsitemtemplate, item.id, item.gencod, item.name, item.shortname, item.price, item.sellunit, item.num));
                }
                $("#goods_table").show();
                statisticsOrders();
            }


            //选择客户
            $("#customer_select").click(function () {
                var iTop = (window.screen.availHeight - 30 - 600) / 2; //获得窗口的垂直位置;
                var iLeft = (window.screen.availWidth - 10 - 750) / 2; //获得窗口的水平位置;
                var url = this.href;
                window.open(url, "选择客户", "height=600, width=750, toolbar=no, menubar=no, resizable=no, location=no, status=no,scrollbars=yes" + ',top=' + iTop + ',left=' + iLeft);
                return false;
            });

            //关联货品切换
            $("#@Html.IdFor(m=>m.IsGoods)").click(function () {
                if (this.checked) {
                    $("#fieldset_goods").show();
                    $("#li_Amount").hide();
                    $("#@Html.IdFor(m => m.Amount)").val("0");
                } else {
                    $("#li_Amount").show();
                    $("#fieldset_goods").hide();
                }
            });


            //选择货品
            $("#goods_select").click(function () {
                var iTop = (window.screen.availHeight - 30 - 600) / 2; //获得窗口的垂直位置;
                var iLeft = (window.screen.availWidth - 10 - 750) / 2; //获得窗口的水平位置;
                var url = this.href;
                var customerId = $("#@Html.IdFor(x=>x.CustomerId)").val();
                if (customerId != "") {
                    url += "/" + customerId;
                }
                window.open(url, "选择发货货品", "height=600, width=750, toolbar=no, menubar=no, resizable=no, location=no, status=no,scrollbars=yes" + ',top=' + iTop + ',left=' + iLeft);

                return false;
            });
            //修改货品数量
            $("#goods_table input[name='num']").live("change", function () {

                if (!new RegExp("^\\d+$").test(this.value)) {
                    this.value = this.value.replace(/\D/g, '');
                    alert("只能输入正整数");
                    return false;
                }
                var goodsid = $(this).attr("goodsid");
                for (var i = 0; i < goodsList.length; i++) {
                    var item = goodsList[i];
                    if (item.id == goodsid) {
                        item.num = $(this).val();
                        break;
                    }
                }
                statisticsOrders();
            });
            


            //修改货品价格
            $("#goods_table input[name='price']").live("change", function () {
                if (!new RegExp("^([1-9]\\d*(\\.\\d{1,2})?|0\\.[1-9]\\d?|0\\.0[1-9]|0|0.0)$").test(this.value)) {
                    this.value = $(this).attr("olevalue");
                    alert("只能输入的数字");
                    return false;
                }
                $(this).attr("olevalue", this.value);
                var goodsid = $(this).attr("goodsid");
                for (var i = 0; i < goodsList.length; i++) {
                    var item = goodsList[i];
                    if (item.id == goodsid) {
                        item.price = $(this).val();
                        break;
                    }
                }
                statisticsOrders();
            });

            //移除货品
            $("#goods_table .action a").live("click", function () {
                var goodsid = $(this).attr("goodsid");
                for (var i = 0; i < goodsList.length; i++) {
                    var item = goodsList[i];
                    if (item.id == goodsid) {
                        goodsList.remove(goodsList.getIndexByValue(item));
                        break;
                    }
                }
                $("#goods_" + goodsid).remove();
                if (goodsList.length == 0) {
                    $("#goods_table").hide();
                }
                statisticsOrders();
                return false;
            });

        });


        //选择/取消货品完成事件
        var goodsCommandCompleted = function (command, itemString) {
            var item = JSON.parse(itemString);
            if (command == "add") {
                goodsList.push(item);
                $("#goods_table").append(String.format(goodsitemtemplate, item.id, item.gencod, item.name, item.shortname, item.price, item.sellunit, item.num));
                if (goodsList.length > 0) {
                    $("#goods_table").show();
                }
            } else if (command == "del") {
                for (var i = 0; i < goodsList.length; i++) {
                    if (goodsList[i].id == item.id) {
                        goodsList.remove(goodsList.getIndexByValue(item));
                        break;
                    }
                }
                $("#goods_" + item.id).remove();
                if (goodsList.length == 0) {
                    $("#goods_table").hide();
                }
            }
            statisticsOrders();
        };
        var statisticsOrders = function () {
            var sum = 0;
            for (var i = 0; i < goodsList.length; i++) {
                var item = goodsList[i];
                sum += new Number(item.price) * new Number(item.num);
            }
            $("#order_sum").html(sum);
            $("#@Html.IdFor(m=>m.Amount)").val(sum);
            $("#@Html.IdFor(m=>m.OrderItems)").val(JSON.stringify(goodsList));
        };

</script>

}
